﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;

namespace SideMan.Models
{
    public class NoteHelper
    {
        public static TextData ToJson(NoteViewModel note)
        {
            var dat = new TextData();

            dat.No = note.No;
            dat.Color = ConvertBrushToString(note.FlagColorBrush);
            dat.ID = note.ID;
            dat.Content = note.RichTextBoxContent;

            //TextRange textRange = new TextRange(note.RichTextBoxContent.ContentStart, note.RichTextBoxContent.ContentEnd);
            //MemoryStream s = new MemoryStream();
            //textRange.Save(s, DataFormats.XamlPackage);
            //dat.Content = Base64Util.Encode(s.ToArray());

            return dat;
        }

        public static NoteViewModel FromData(TextData textData)
        {
            NoteViewModel note = new NoteViewModel();

            //MemoryStream s = new MemoryStream(Convert.FromBase64String(Convert.ToString(textData.Content)));
            //TextRange TR = new TextRange(note.RichTextBoxContent.ContentStart, note.RichTextBoxContent.ContentEnd);
            //TR.Load(s, DataFormats.XamlPackage);

            note.ID = textData.ID;
            note.No = textData.No;
            note.RichTextBoxContent = textData.Content;

            note.FlagColorBrush = ConvertStringToBrush(textData.Color);

            return note;
        }


        public static void Save(TextData[] textDatas, string dataPath)
        {
            foreach (var item in textDatas)
            {
                // Serialize the array to JSON
                string jsonString = JsonConvert.SerializeObject(item, Formatting.Indented);

                var path = Path.Combine(dataPath, item.ID);

                // Write the JSON string to the file asynchronously
                WriteJsonToFile(fillWithExt(path, ".json"), jsonString);
            }
        }
        static string fillWithExt(string path, string ext)
        {
            if (string.IsNullOrEmpty(Path.GetExtension(path)))
            {
                path += ext;
            }
            return path;
        }
        static async Task WriteJsonToFileAsync(string filePath, string jsonString)
        {
            using (StreamWriter writer = new StreamWriter(filePath))
            {
                await writer.WriteAsync(jsonString);
            }
        }
        static void WriteJsonToFile(string filePath, string jsonString)
        {
            using (StreamWriter writer = new StreamWriter(filePath))
            {
                writer.Write(jsonString);
            }
        }
        static string ConvertBrushToString(SolidColorBrush brush)
        {
            // Extract color information from the SolidColorBrush
            Color color = brush.Color;

            // Convert Color to a string representation
            string colorString = $"{color.A:X2}{color.R:X2}{color.G:X2}{color.B:X2}";

            return colorString;
        }
        static SolidColorBrush ConvertStringToBrush(string colorString)
        {
            if (colorString.Length != 8) // The string should represent ARGB (8 characters)
            {
                throw new ArgumentException("Invalid color string format");
            }

            // Parse the color components from the string
            byte a = Convert.ToByte(colorString.Substring(0, 2), 16);
            byte r = Convert.ToByte(colorString.Substring(2, 2), 16);
            byte g = Convert.ToByte(colorString.Substring(4, 2), 16);
            byte b = Convert.ToByte(colorString.Substring(6, 2), 16);

            // Create a new SolidColorBrush
            SolidColorBrush brush = new SolidColorBrush(Color.FromArgb(a, r, g, b));

            return brush;
        }
    }
}
